import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface UserInfo {
  username: string
  role: 'visitor' | 'admin'
}

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<UserInfo | null>(null)
    const isLoggedIn = ref(false)

    function login(user: UserInfo) {
      userInfo.value = user
      isLoggedIn.value = true
    }

    function logout() {
      userInfo.value = null
      isLoggedIn.value = false
    }

    return {
      userInfo,
      isLoggedIn,
      login,
      logout
    }
  },
  {
    persist: true
  }
)